import 'package:flutter/material.dart';
import 'package:window_manager/window_manager.dart';

class CustomWindowFrame extends StatelessWidget {
  final Widget child;
  final String title;
  final Color? backgroundColor;

  const CustomWindowFrame({
    super.key,
    required this.child,
    this.title = '黄裳元吉代办',
    this.backgroundColor,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        // 自定义标题栏
        MouseRegion(
          cursor: SystemMouseCursors.move,
          child: GestureDetector(
            behavior: HitTestBehavior.translucent,
            onPanStart: (details) {
              windowManager.startDragging();
            },
            child: Material(
              color: backgroundColor ?? Theme.of(context).colorScheme.primary,
              child: Container(
                height: 32,
                child: Row(
                  children: [
                    const SizedBox(width: 12),
                    Text(
                      title,
                      style: TextStyle(
                        color: Theme.of(context).colorScheme.onPrimary,
                        fontSize: 14,
                      ),
                    ),
                    const Spacer(),
                    // 最小化按钮
                    _WindowButton(
                      icon: Icons.remove,
                      onPressed: () async {
                        await windowManager.minimize();
                      },
                      iconColor: Theme.of(context).colorScheme.onPrimary,
                    ),
                    // 最大化/还原按钮
                    _WindowButton(
                      icon: Icons.crop_square,
                      onPressed: () async {
                        if (await windowManager.isMaximized()) {
                          await windowManager.restore();
                        } else {
                          await windowManager.maximize();
                        }
                      },
                      iconColor: Theme.of(context).colorScheme.onPrimary,
                    ),
                    // 关闭按钮
                    _WindowButton(
                      icon: Icons.close,
                      onPressed: () async {
                        await windowManager.close();
                      },
                      hoverColor: Colors.red,
                      iconColor: Theme.of(context).colorScheme.onPrimary,
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
        // 主要内容
        Expanded(child: child),
      ],
    );
  }
}

class _WindowButton extends StatelessWidget {
  final IconData icon;
  final VoidCallback onPressed;
  final Color? hoverColor;
  final Color? iconColor;

  const _WindowButton({
    required this.icon,
    required this.onPressed,
    this.hoverColor,
    this.iconColor,
  });

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 46,
      height: double.infinity,
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: onPressed,
          hoverColor: hoverColor ?? Colors.black12,
          child: Icon(
            icon,
            size: 16,
            color: iconColor ?? Theme.of(context).colorScheme.onSurface,
          ),
        ),
      ),
    );
  }
} 